package ProjectViewer;

import gnu.io.CommPortIdentifier;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import System.Settings_System;

public class Panel_ProjectData extends JPanel implements FocusListener{

	
	private static final String [] [] MessageName =
		{{"Project name","path","load","link","upload","...","Basis addres"},
		{"Projektname","Pfad","Laden","linken","upload","...","Basis adresse"}};	
		
		
	private static final int String_ProjectNameID					=0;
	private static final int String_PathID							=String_ProjectNameID+1; 
	private static final int String_LoadDataID						=String_PathID+1; 
	private static final int String_LinkDataID						=String_LoadDataID+1;
	private static final int String_UploadDataID					=String_LinkDataID+1; 
	private static final int String_SelectID						=String_UploadDataID+1;
	private static final int String_BasisAdressID					=String_SelectID+1;
	
	JTextField projectPath;
	JTextField projectName;
	
	JTextField basisAdress;
	
	JComboBox portSelector;	
	
	Window_ProjectView myProjectWindow;
	
public Panel_ProjectData(Window_ProjectView ProjectWindow)
{
	
	myProjectWindow=ProjectWindow;
	setSize(900,100);
	build();
}



public void build()
{
	JLabel tmpLabel;
	
	
	setBorder(new TitledBorder(new LineBorder(Color.black,2),MessageName [Settings_System.GetActiveLanguage()] [String_ProjectNameID]));
	setLayout(null);
	
	tmpLabel= new JLabel (MessageName [Settings_System.GetActiveLanguage()] [String_PathID]);
	tmpLabel.setBounds(10, 20, 100, 20);
	add (tmpLabel);

	
	tmpLabel= new JLabel (MessageName [Settings_System.GetActiveLanguage()] [String_ProjectNameID]);
	tmpLabel.setBounds(10, 60, 100, 20);
	add (tmpLabel);

	
	
	tmpLabel= new JLabel (MessageName [Settings_System.GetActiveLanguage()] [String_BasisAdressID]);
	tmpLabel.setBounds(300, 60, 100, 20);
	add (tmpLabel);
	
	projectPath= new JTextField("D:/Projekte/GamePad/Java/JVM_Test1/") ;
	projectPath.setBounds(100, 20, 300, 20);
	projectPath.setActionCommand(Window_ProjectView.cmd_setPath);
	projectPath.addActionListener(myProjectWindow);
	projectPath.addFocusListener(this);
	add (projectPath);
	
	projectName= new JTextField("new");
	projectName.setBounds(100, 60, 150, 20);
	projectName.setActionCommand(Window_ProjectView.cmd_renameProject);
	projectName.addFocusListener(this);
	projectName.addActionListener(myProjectWindow);
	add (projectName);
	
	
	
	basisAdress= new JTextField("0x0");
	basisAdress.setBounds(400, 60, 150, 20);
	basisAdress.setActionCommand(Window_ProjectView.cmd_setBaseAddres);
	basisAdress.addFocusListener(this);
	basisAdress.addActionListener(myProjectWindow);
	add (basisAdress);
	
	
	JButton tmpButton;
	tmpButton= new JButton(MessageName [Settings_System.GetActiveLanguage()] [String_LoadDataID]);
	tmpButton.setActionCommand(Window_ProjectView.cmd_load);
	tmpButton.addActionListener(myProjectWindow);
	tmpButton.setBounds(480, 20, 100, 30);
	add(tmpButton);
	
	
	
	tmpButton= new JButton(MessageName [Settings_System.GetActiveLanguage()] [String_LinkDataID]);
	tmpButton.setActionCommand(Window_ProjectView.cmd_link);
	tmpButton.addActionListener(myProjectWindow);
	tmpButton.setBounds(590, 20, 100, 30);
	add(tmpButton);
	
		

	tmpButton= new JButton(MessageName [Settings_System.GetActiveLanguage()] [String_UploadDataID]);
	tmpButton.setActionCommand(Window_ProjectView.cmd_upload);
	tmpButton.addActionListener(myProjectWindow);
	tmpButton.setBounds(700, 20, 100, 30);
	add(tmpButton);
	
	
	tmpButton= new JButton(MessageName [Settings_System.GetActiveLanguage()] [String_SelectID]);
	tmpButton.setActionCommand(Window_ProjectView.cmd_selectPath);
	tmpButton.addActionListener(myProjectWindow);
	tmpButton.setBounds(410, 15, 50, 30);
	add(tmpButton);
	

	portSelector= new JComboBox();
	portSelector.setBounds(700,60,100,20);
	add(portSelector);

	FindSerialPorts();
	
	
	
}



/**
 * @author Tavin
 * 
 * date 2001.4.7
 * @return
 */

public String getProjectName()
{
	return(projectName.getText());
}


/**
 * @author Tavin
 * 
 * Date 2011.04.08
 * 
 * @param newName 
 */

public void setProjectName(String newName)
{
	projectName.setText(newName);
}


/**
 * @author Tavin
 * 
 * date 2001.4.7
 * 
 * @return
 */

public String getProjectPath()
{
	return(projectPath.getText());
	
}


/**
 * @author Tavin
 * 
 * Date 2001.4.7
 * 
 * @param newPath
 */
public void setProjectPath(String newPath)
{
	projectPath.setText(newPath);
	
}



public int getBaseAdress()
{
	int baseAdress;
	String tmpString;
	
	baseAdress=0;
	
	tmpString=basisAdress.getText();
	
	if (tmpString.startsWith("0x"))
		tmpString=tmpString.substring(2);
	
	
	if (tmpString!="")
		{
		baseAdress=Integer.parseInt(tmpString, 16);
		}
	
	System.out.print(baseAdress);
	
	return(baseAdress);
	
}



public String getSerialPort()
{
	return(portSelector.getSelectedItem().toString());
	
}


/****************************************************************************/
/*																			*/
/*	FindSerialPorts										Author OG			*/
/*																			*/
/*	Description																*/
/*																			*/
/*	Version	0.5				Date 2009.05.11				Ststus : i.d.		*/
/*																			*/
/****************************************************************************/

void FindSerialPorts()
{
	portSelector.removeAllItems();
	
    java.util.Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
    
    while ( portEnum.hasMoreElements() ) 
    	{
        CommPortIdentifier portIdentifier = (CommPortIdentifier) portEnum.nextElement();

        if (portIdentifier.getPortType()==CommPortIdentifier.PORT_SERIAL)
        	{
        	portSelector.addItem(portIdentifier.getName());
        	}
    	}

}
		



@Override
public void focusGained(FocusEvent arg0) {
	// TODO Auto-generated method stub
	
}



@Override
public void focusLost(FocusEvent event) {
	// TODO Auto-generated method stub
	
	
	JTextField textField;
	
	
	if (event.getComponent().getClass().getName().equals(JTextField.class.getName()))
	{
		textField=(JTextField)event.getComponent();
		textField.postActionEvent();
	}


	System.out.println("here");
	

}

}
